<?php
class MessageController extends Zend_Controller_Action
{

    private $user_id;

    //list users
    

    function init () {
        $this->user_id = Zend_Auth::getInstance()->getIdentity()->id;
        
        $init = new Model_Init($this);
    }

    function indexAction () {
        //name is request, 'cause $this->url always need to set name to null other it will use it in every url
        $page = $this->_getParam('request');
        
        $message = new Model_Message();
        $this->view->messages = $message->getMessages($this->user_id, $page);
    
    }

    function readAction () {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        $message_id = $this->_getParam('request');
        $message = new Model_Message($message_id);
        $message->readMessage($this->user_id);
        
        $this->_helper->redirector('index', 'message');
    }

    function messageAction () {
        $message_id = $this->_getParam('request');
        $message = new Model_Message($message_id);
        
        $message->setReadMessage($this->user_id);
        $this->view->message = $message->message($this->user_id);
    }

    function deleteAction () {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        $message_id = $this->_getParam('request');
        $message = new Model_Message($message_id);
        $message->removeMessage($this->user_id);
        
        $this->_helper->redirector('index', 'message');
    
    }

    function sendAction () {
//        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        $form = new Form_Message();
        $to = $this->_getParam('request');
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()
                ->getPost())) {
                
                $message = new Model_Message();
                $message->fill($form->getValues());
                $message->user_from = $this->user_id;
                $message->user_id = $to;
                
                $message->date = date("Y-m-d H:i:s", time());
                $message->save();
                
                $user = new Model_User($to);
                
                $redir = $this->_getParam('redirect');
                
                if($redir == 'index'){
                    $this->_helper->redirector(null, $user->login);
                }else{
                    $this->_helper->redirector(null, $redir );
                }
            }
        }
        $this->view->answer_form = $form;
    }

    function feedbackAction () {
        $form = new Form_Feedback();
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()
                ->getPost())) {
                
                $mail = new Zend_Mail('utf-8');
                $mail->setBodyHtml($form->message);
                $mail->setFrom(Zend_Auth::getInstance()->getIdentity()->email);
                $mail->addTo('support@people-z.ru');
                $mail->setSubject("Feedback");
                $mail->send();
                echo "Thx for feedback.";
            
            }
        }
        $this->view->form = $form;
    }

}









